import re
from datetime import datetime

from dateutil.relativedelta import relativedelta
from flask import Blueprint, Response, render_template, request, redirect, make_response

from sqlalchemy import text, select

from models.RentRecord import RentRecord
from models.House import House
from models.User import User
from settiings  import db
from flask import jsonify
import json
import time
import uuid

bp = Blueprint('modify_page', __name__,url_prefix='/modify')

@bp.route('/userinfo/name',methods=['POST'])
def modify_userinfo_name():
    current_username = request.cookies.get('username')
    if not current_username:
        return jsonify({'ok': 0, 'msg': '未登录或会话过期'}), 401

    new_name = request.form.get('name')
    if not new_name:
        return jsonify({'ok': 0, 'msg': '缺少 "name" 字段'}), 400

    user = User.query.filter_by(name=new_name).first()
    if user and user.name != current_username:
        return jsonify({'ok': 0, 'msg': '用户名已存在'})

    current_user = User.query.filter_by(name=current_username).first()
    if not current_user:
        return jsonify({'ok': 0, 'msg': '当前用户不存在'}), 404

    current_user.name = new_name
    db.session.commit()
    response = make_response(jsonify({'ok': 1, 'msg': '更新成功'}))
    response.set_cookie('username', new_name)
    return response


# 修改地址
@bp.route('/userinfo/addr', methods=['POST'])
def modify_userinfo_addr():
    current_username = request.cookies.get('username')
    addr = request.form.get('addr')
    if not addr:
        return jsonify({'ok': 0, 'msg': '缺少必要参数'}), 400
    if not re.match(r'^[\u4e00-\u9fa5a-zA-Z0-9]+$', addr) or len(addr) < 6 or len(addr) > 15:
        return jsonify({'ok': 0, 'msg': '地址格式不正确，需要6-15位中文、字母或数字'}), 400

    user = User.query.filter_by(name=current_username).first()
    if not user:
        return jsonify({'ok': 0, 'msg': '用户不存在'}), 404

    user.addr = addr
    db.session.commit()

    return jsonify({'ok': 1, 'msg': '地址更新成功', 'data': {'addr': addr}})


# 修改密码
@bp.route('/userinfo/password', methods=['POST'])
def modify_userinfo_password():
    current_username = request.cookies.get('username')
    if not current_username:
        return jsonify({'ok': 0, 'msg': '未登录或会话已过期'}), 401

    password = request.form.get('pd')
    if not password:
        return jsonify({'ok': 0, 'msg': '缺少必要参数'}), 400

    if not re.match(r'^[a-zA-Z0-9]+$', password) or len(password) < 6 or len(password) > 15:
        return jsonify({'ok': 0, 'msg': '密码格式不正确，需要6-15位字母或数字'}), 400
    user = User.query.filter_by(name=current_username).first()
    user.password = password
    db.session.commit()

    return jsonify({'ok': 1, 'msg': '密码更新成功'})


# 修改邮箱
@bp.route('/userinfo/email', methods=['POST'])
def modify_userinfo_email():
    current_username = request.cookies.get('username')
    email = request.form.get('email')

    email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    if not re.match(email_regex, email):
        return jsonify({'ok': 0, 'msg': '邮箱格式不正确'}), 400

    user = User.query.filter_by(name=current_username).first()
    user.email = email
    db.session.commit()
    return jsonify({'ok': 1, 'msg': '邮箱更新成功', 'data': {'email': email}})
